home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9449 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  75 lines

  1. Path: solon.com!not-for-mail
  2. From: seebs@solutions.solon.com (Peter Seebach)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: malloc question
  5. Date: 10 Mar 1996 13:29:40 -0600
  6. Organization: Usenet Fact Police (Undercover)
  7. Message-ID: <4hvaj4$laj@solutions.solon.com>
  8. References: <4htonk$350@news.hklink.net> <4huctt$arv@sparcserver.lrz-muenchen.de> <314318AF.30F@iperbole.bologna.it>
  9. NNTP-Posting-Host: solutions.solon.com
  10.  
  11. In article <314318AF.30F@iperbole.bologna.it>,
  12. Enrico Persiani  <vos0225@iperbole.bologna.it> wrote:
  13. >Error?!? Why do you think that casting is an error? The standard ANSI
  14. >runtime library says:
  15.  
  16. He didn't say it was an error, he said it could *hide* an error.
  17.  
  18. >So, if he needs to use that pointer returned by malloc function he have to
  19. >convert it from a 'void' pointer to a 'item' pointer ( PITEM ).
  20.  
  21. Bullshit.  The only time the cast would be necessary is in the args to
  22. a variadic function which does not expect a (void *).
  23.  
  24. >Many compilers don't need the use of casting. It's only a good programming
  25. >style.
  26.  
  27. No compiler can require it, and it is arguably *bad* style.
  28.  
  29. >But remember: malloc returns a 'void' pointer because it doesn't know
  30. >wich kind of data you'll store in the allocated mem block!
  31.  
  32. True.
  33.  
  34. >C++ compilers are more rigorous! But if you write a strict-ANSI C program a
  35. >c++ compiler won't return any error...
  36.  
  37. Bullshit.
  38.  
  39.     #include <stdlib.h>
  40.  
  41.     int
  42.     main(void) {
  43.         char *s = malloc(10);
  44.         return 0;
  45.     }
  46.  
  47. is *completely* legal ANSI, and *CANNOT* be rejected by any legal C compiler.
  48. It *MUST* be diagnosed by a C++ compiler.  (Every C++ compiler I've ever heard
  49. of would reject it, but of course, an implementation may *accept* any program,
  50. as long as there is a diagnostic.)
  51.  
  52. C++ changes the semantics of (void *).
  53.  
  54. To rehast, the error that the cast hides is that:
  55.     /* note *no* inclusion of <stdlib.h> */
  56.     int main(void) {
  57.         char *s = malloc(10);
  58.         return 0;
  59.     }
  60. *must* get a warning, and
  61.     /* note *no* inclusion of <stdlib.h> */
  62.     int main(void) {
  63.         char *s = (char *) malloc(10);
  64.         return 0;
  65.     }
  66. is not required to, because, as Kurt correctly stated, *the cast can hide
  67. a potential error*.
  68.  
  69. -s
  70. -- 
  71. Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
  72. C/Unix wizard -- C/Unix questions? Send mail for help.  No, really!
  73. FUCK the communications decency act.  Goddamned government.  [literally.]
  74. The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
  75.